package top.lcm889.oj.offer.按数据结构分类.链表;

/**
 * @author Coder-lcm
 * @date 2021/5/3
 */
public class OF50_第一个只出现一次的字符 {


    public char firstUniqChar(String s) {
        int[] times = new int[26];
        int[] firstIndexs = new int[26];
        char[] chars = s.toCharArray();
        int cur = 0;
        for (int i = 0; i < chars.length; i++) {
            cur = chars[i] - 'a';
            if (times[cur] == 0) {
                firstIndexs[cur] = i;
            }else {
                firstIndexs[cur] = -1;
            }
            times[cur]++;
        }

        int firstIndex = firstIndexs[0];
        int index = 0;
        for (int i = 1; i < 26; i++) {
            if (firstIndexs[i] >= 0 && firstIndexs[i] < firstIndex) {
                index = i;
                firstIndex = firstIndexs[i];
            }
        }
        return (char) (index + 'a');

    }

    public static void main(String[] args) {

    }
}
